﻿using BusinessService.Plugin.DataSource.Sqlite;
using Elephant.FormUtils;
using EMSSevice;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsForm.FormDtos;
namespace WindowsForm.Forms
{
    public partial class JsonBuild : UserControl
    {
        private SqliteLibrary _repSqlite => Program.IOC.GetRequiredService<SqliteLibrary>();
        public JsonBuild()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 表格刷新事件
        /// </summary>
        public static Action RefreshTable;
        private void JsonBuild_Load(object sender, EventArgs e)
        {
            updateTable();
            RefreshTable += updateTable;
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            发送关系设置 jsonSrtuctSet = new 发送关系设置();
            jsonSrtuctSet.Show();
        }
        private void updateTable()
        {

            var result = _repSqlite.JsonQuery();
            var table = result.Select(x => new ParameterSettingsDto
            {
                ID = x.ID,
                Name = x.Name,
                UploadTarget = x.UploadTarget,
                UploadTargetType = x.UploadTargetType,
                DataSourceList = x.DataSourceList,
                CreateTime = x.CreateTime,
                Btns = new AntdUI.CellLink[] {
                    new AntdUI.CellButton("edit","编辑",AntdUI.TTypeMini.Primary) {  Ghost = true,BorderWidth=1 },
                    new AntdUI.CellButton("delete","删除",AntdUI.TTypeMini.Error) {  Ghost = true ,BorderWidth=1}
                    },
            }).ToList();
            table1.DataSource = table;
            table1.Columns = new AntdUI.Column[] {
                       new AntdUI.Column("ID", "ID"),
                       new AntdUI.Column("Name","Json名称"),
                       new AntdUI.Column("DataSourceList","数据源集合"),
                       new AntdUI.Column("UploadTarget","上传通道"),
                         new AntdUI.Column("UploadTargetType","上传通道类型"),
                       new AntdUI.Column("CreateTime", "创建时间"),
                        new AntdUI.Column("Btns", "操作") ,
            };// 添加表头，绑定模型名称
        }
        private void button2_Click(object sender, EventArgs e)
        {
            updateTable();
        }
        private void table1_CellButtonClick(object sender, AntdUI.CellLink btn, MouseEventArgs args, object record, int rowIndex, int columnIndex)
        {
            if (SqlFunc.IsNullOrEmpty(btn)) return;
            if (btn.Text.Equals("删除"))
            {
                var selectRow = (ParameterSettingsDto)record;
                if (selectRow != null)
                {

                    var dialogResult = MessageBox.Show(
                                                "确定删除此数据源吗？",
                                                 " 警告",
                                                 MessageBoxButtons.OKCancel,
                                                 MessageBoxIcon.Warning);
                    if (dialogResult == DialogResult.Cancel) return;
                    _repSqlite.jsonStructDelete(selectRow.ID);//删除指定行
                    updateTable();//刷新表格内容
                };
            }
            if (btn.Text.Equals("编辑"))
            {
                发送关系设置 jsonEditBox = new();
                var selectRow = (ParameterSettingsDto)record;
                jsonEditBox.Tag = selectRow.ID;
                jsonEditBox.ShowDialog();
            }
        }
        private void table1_CellClick(object sender, MouseEventArgs args, object record, int rowIndex, int columnIndex, Rectangle rect)
        {
        }
    }
}
